package adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import bean.LunTanOffRecyBean;
import jiyun.com.yiyanmeng.R;

/**
 * Created by zhang'lin'lin on 2019/11/7.
 */

public class LunTanOffAdapter extends RecyclerView.Adapter {
    private List<LunTanOffRecyBean.InfoBean.PlateBean> plate;//官方论坛_平台
    private List <LunTanOffRecyBean.InfoBean.ArticleBean> article;//官方论坛_文章
    private Context context;
    private static final int VIEW_0=0;
    private static final int VIEW_1=1;

    public LunTanOffAdapter(List <LunTanOffRecyBean.InfoBean.PlateBean> plate, List <LunTanOffRecyBean.InfoBean.ArticleBean> article, Context context) {
        this.plate = plate;
        this.article = article;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==0){
            View view = LayoutInflater.from(context).inflate(R.layout.item_luntanoff_top, parent, false);
            return new TopVH(view);
        }else{
            View view = LayoutInflater.from(context).inflate(R.layout.item_luntanoff_bottom, parent, false);
            return new BottomVH(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        //圆角
        RoundedCorners roundedCorners = new RoundedCorners(30);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        if (type==0){
            TopVH topVH= (TopVH) holder;
            topVH.tv1.setText(plate.get(position).getTitle());
            topVH.tv3.setText(plate.get(position).getNum());
            Glide.with(context).load(plate.get(position).getImage()).apply(options).into(topVH.iv);
        }else if (type==1){
            BottomVH bottomVH= (BottomVH) holder;
            bottomVH.tv1.setText(article.get(position).getAuthName());
            bottomVH.tv2.setText(article.get(position).getAddtime());
            bottomVH.tv3.setText(article.get(position).getTitle());
            bottomVH.tv4.setText(article.get(position).getContent());
            bottomVH.tv5.setText(article.get(position).getPing_num());
            Glide.with(context).load(article.get(position).getAuthImg()).apply(options).into(bottomVH.iv);
        }
    }

    @Override
    public int getItemCount() {
       return article.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0||position==1||position==2){
            return VIEW_0;
        }
        return VIEW_1;
    }

    class TopVH extends RecyclerView.ViewHolder{

        private final ImageView iv;
        private final TextView tv1;
        private final TextView tv2;
        private final TextView tv3;

        public TopVH(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);
            tv1 = itemView.findViewById(R.id.tv1);
            tv2 = itemView.findViewById(R.id.tv2);
            tv3 = itemView.findViewById(R.id.tv3);
        }
    }

    class BottomVH extends RecyclerView.ViewHolder{

        private final ImageView iv;
        private final TextView tv1;
        private final TextView tv2;
        private final TextView tv3;
        private final TextView tv4;
        private final TextView tv5;

        public BottomVH(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);
            tv1 = itemView.findViewById(R.id.tv1);
            tv2 = itemView.findViewById(R.id.tv2);
            tv3 = itemView.findViewById(R.id.tv3);
            tv4 = itemView.findViewById(R.id.tv4);
            tv5 = itemView.findViewById(R.id.tv5);
        }
    }

}
